<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'RepositoryController@index');

Auth::routes();
Route::get('logout', 'Auth\LoginController@logout')->name('logout');

//Route::get('/home', 'HomeController@index')->name('home');
Route::middleware(['auth'])->group(function () {
    Route::resource('repositories', 'RepositoryController');
    Route::prefix('repositories/{repository}')->name('repositories.')->group(function () {
        Route::get('/download', 'RepositoryController@download')->name('download')->middleware(['throttle:1,1']);
        Route::post('/like', 'RepositoryController@like')->name('like');
        Route::prefix('/forks')->name('forks.')->group(function () {
            Route::get('/', 'RepositoryForkController@index')->name('index');
            Route::get('/create', 'RepositoryForkController@create')->name('create')->middleware(['can:repositories.fork,repository']);
            Route::post('/', 'RepositoryForkController@store')->name('store')->middleware(['can:repositories.fork,repository']);
        });

        Route::prefix('/pulls')->name('pulls.')->group(function () {
            Route::get('/', 'RepositoryPullController@index')->name('index');
            Route::post('/', 'RepositoryPullController@merge')->name('merge')->middleware(['can:repositories.pull,repository']);
        });

        Route::prefix('/sequences')->name('sequences.')->group(function () {
            Route::get('/{sequence}', 'RepositorySequenceController@index')->name('index');
            Route::put('/{sequence}/switch-to-chapter', 'RepositorySequenceController@switchChapter')->name('switch-chapter')->middleware(['can:repositories.switch-chapter,repository']);
        });
        Route::prefix('/chapters')->name('chapters.')->group(function () {

            Route::get('/{chapter}/edit', 'ChapterController@edit')->name('edit')->middleware(['can:repositories.create-chapter,repository']);
            Route::post('/{chapter}/push', 'ChapterController@push')->name('push')->middleware(['can:repositories.push,repository']);
            Route::get('/create', 'ChapterController@create')->name('create')->middleware(['can:repositories.create-chapter,repository']);
            Route::post('', 'ChapterController@store')->name('store')->middleware(['can:repositories.create-chapter,repository']);
        });
        Route::resource('issues', 'IssueController')->only(['index', 'store']);
    });
    Route::prefix('/search')->middleware(['throttle:3,1',])->group(function () {
        Route::get('/repositories', 'SearchController@repositories');
        Route::get('/chapters', 'SearchController@chapters')->name('chapters.search');
        Route::get('/users', 'SearchController@users')->name('users.search');
    });
    Route::prefix('/chapters')->name('chapters.')->group(function () {
        Route::delete('/{chapter}', 'ChapterController@destroy')->name('destroy')->middleware(['can:chapters.delete,chapter',]);

        Route::post('/{chapter}/like', 'ChapterController@like')->name('like');
        Route::post('/{chapter}/dislike', 'ChapterController@dislike')->name('dislike');
    });
    Route::post('/chapters/{chapter}/comments', 'CommentController@store')->name('comments.store');
    Route::delete('/comments/{comment}', 'CommentController@destroy')->name('comments.destroy')->middleware(['can:comments.delete,comment']);
    Route::prefix('/users')->name('users.')->group(function () {
        Route::post('/{user}/like', 'UserController@like')->name('like');
        Route::get('/{user}/repositories', 'UserRepositoriesController')->name('repositories');
        Route::get('/{user}/chapters', 'UserChaptersController')->name('chapters');
        Route::get('/{user}/reward', 'UserRewardController@edit')->name('reward.edit')->middleware(['can:users.update,user',]);
        Route::put('/{user}/reward', 'UserRewardController@update')->name('reward.update')->middleware(['can:users.update,user',]);
        Route::get('/{user}/password', 'UserPasswordController@edit')->name('password.edit')->middleware(['can:users.update,user',]);
        Route::put('/{user}/password', 'UserPasswordController@update')->name('password.update')->middleware(['can:users.update,user',]);
    });
    Route::resource('users', 'UserController')->only(['show']);
    Route::prefix('/tieba')->name('tieba.')->group(function () {
        Route::get('/', 'TiebaController@index')->name('index');
        Route::post('/', 'TiebaController@store')->name('store');
        Route::get('/{id}', 'TiebaController@show')->name('show');
        Route::get('/{id}/status', 'TiebaController@status')->name('status');
    });
});

Route::namespace('Rank')->prefix('/rank')->name('rank.')->group(function () {
    Route::view('/', 'resources.rank.index')->name('index');
    Route::get('/user-repositories-count', 'UserRepositoriesCount')->name('user-repositories-count');
    Route::get('/user-chapters-count', 'UserChaptersCount')->name('user-chapters-count');
    Route::get('/repositories-likes-count', 'RepositoriesLikesCount')->name('repositories-likes-count');
    Route::get('/repositories-length', 'RepositoriesLength')->name('repositories-length');
    Route::get('/repositories-views', 'RepositoriesViews')->name('repositories-views');
});